package com.qc31.monitorlib.ui.alarms

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.rx.queryThrottle
import com.qc31.baselib.utils.loadStringDImgError
import com.qc31.baselib.utils.startActivityTo
import com.qc31.comlib.entity.AlarmMedia
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.FragmentAlarmVideoPhotoBinding
import com.qc31.monitorlib.ui.LocationDialogActivity
import com.qc31.monitorlib.ui.adapter.MediaAdapter

/** @Author Jasper @Time 2020/12/1 @Desc 报警详情照片 */
class AlarmPhotoFragment :
    LazyFragment<FragmentAlarmVideoPhotoBinding>(FragmentAlarmVideoPhotoBinding::inflate) {

    private val mAdapter by lazy { MediaAdapter(false) }
    private var lat: Double = 0.0
    private var lng: Double = 0.0


    override fun initView(view: View) {
        binding.includeRecycler.recyclerView.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        binding.includeRecycler.recyclerView.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val entity = mAdapter.data[position]
            binding.ivAlarmMedia.loadStringDImgError(requireContext(), entity.fullLink)
//            setVideoDesc(entity)
        }
    }

    override fun initData() {}

    override fun initEvent() {
        binding.tvAlarmAddress.queryThrottle().autoDispose(scopeProvider).subscribe {
            startActivityTo<LocationDialogActivity>(requireContext()) {
                putExtra("lat", lat)
                putExtra("lng", lng)
            }
        }
    }

    fun setVideoData(
        data: MutableList<AlarmMedia.MediaInfo>,
        entity: AlarmMedia.MediaInfo
    ) {
        setVideoDesc(entity)
        mAdapter.index = 0
        mAdapter.setList(data)
        lat = entity.glat.toDouble()
        lng = entity.glng.toDouble()
        if (data.size > 0)
            binding.ivAlarmMedia.loadStringDImgError(requireContext(), data[0].fullLink)
    }

    @SuppressLint("SetTextI18n")
    private fun setVideoDesc(entity: AlarmMedia.MediaInfo) {
        binding.lrTvAlarmTime.setRightText(entity.time)
        binding.lrTvAlarmSpeed.setRightText("${entity.speed} ${getString(R.string.unit_speed)}")

        binding.lrTvAlarmReason.setRightText(entity.reason)
        binding.tvAlarmAddress.text = entity.addr

        binding.ivAlarmMedia.loadStringDImgError(requireContext(), entity.fullLink)

    }
}